<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/6/23-13:54
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */

namespace App\Http\Controllers\Web\Buyer;

use App\ConstDir\ErrorConst;
use App\Repository\Models\Orders;
use App\Repository\Repositories\Interfaces\OrderQualificationBidRepository;
use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class ServiceOrdersController extends BaseController
{
    protected $serviceOrders_repo;
    
    protected $request;
    protected $user_id;

    public function __construct(Request $request,OrderQualificationBidRepository $serviceOrders_repo)
    {
        parent::__construct();
        $this->request = $request;
        $this->user_id = array_get($this->user, 'id');
        $this->serviceOrders_repo = $serviceOrders_repo;

        //用户只能操作自身所属数据
        $this->serviceOrders_repo->scopeQuery(function ($query) {
            return $query->where('user_id', $this->user_id);
        });
    }

    public function index()
    {
        $orders = $this->serviceOrders_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $orders, true);
    }
    public function show($id)
    {
        $order = $this->serviceOrders_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $order['data']);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'order_status' => 'required'
        ]);
        $data = $request->all();

        $ret = $this->serviceOrders_repo->create($data);

        return $this->noDataReturn($ret ? true : false);
    }

    public function update($id)
    {
        $this->validate($this->request, [
            //'expect_date' => 'date_format:Y/m/d',// 期望交付时间
            'remark' => 'string'// 备注信息
        ]);

        $data = $this->request->all(['expect_date', 'remark']);

        $ret = $this->serviceOrders_repo->update($data, $id);

        return $this->noDataReturn($ret ? true : false);
    }

    public function delete($id)
    {
        //用户无删除订单操作，以下作为样例
        $ret = $this->serviceOrders_repo->delete($id);

        return $this->noDataReturn($ret ? true : false);
    }
}